﻿using System;
using System.Collections.Generic;
using UnityEngine;
public class Server : Singleton<Server>, IServer
{


    IClient _client;

    Dictionary<Type, Action<Cmd>> _parser = new Dictionary<Type, Action<Cmd>>();

    public Player curPlayer;

    public Server()
    {
        _parser.Add(typeof(LoginCmd), CmdParser.OnLogin);
        _parser.Add(typeof(SelectRoleCmd), CmdParser.onSelectRole);
    }

    public void Connect(IClient client)
    {
        _client = client;
    }

    public void Receive(Cmd cmd)
    {
        //_client.sendCmd(cmd);
        Debug.Log("服务器收到消息" + cmd.GetType());

        Action<Cmd> func;
        if (_parser.TryGetValue(cmd.GetType(), out func))
        {
            //if (_parser.TryGetValue(cmd.GetType(),out func))
            //{
            if (func != null)
            {
                func(cmd);
            }
            //}
        }

    }

    public void sendCmd(Cmd cmd)
    {
        _client.Receive(cmd);
    }
}
